using System;
using System.CodeDom;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CleanManager : MonoBehaviour
{
    public float DirtAmountTotal;
    public float DirtAmount;

    public Texture2D DirtMaskTextureBase;
    public Material Material;
    [SerializeField] private Texture2D _dirtBrush;

    public Texture2D DirtMaskTexture { get; set; }

    private void Awake()
    {
        DirtMaskTexture = new Texture2D(DirtMaskTextureBase.width, DirtMaskTextureBase.height);
        DirtMaskTexture.SetPixels(DirtMaskTextureBase.GetPixels()); //SetPixels:设置一个像素颜色块
        DirtMaskTexture.Apply(); //实际上应用所有之前的SetPixel和SetPixels更改。

        Material.SetTexture("_DirtMask", DirtMaskTexture);
        //Material.SetFloat("_Value", 0);
        DirtAmountTotal = 0f;
        for (int x = 0; x < DirtMaskTextureBase.width; x++)
        {
            for (int y = 0; y < DirtMaskTextureBase.height; y++)
            {
                DirtAmountTotal += DirtMaskTextureBase.GetPixel(x, y).g;
            }
        }

        DirtAmount = DirtAmountTotal;
    }

//     void Update()
//     {
// #if UNITY_EDITOR
//         if (Input.GetMouseButton(0))
//         {
//             var position = Input.mousePosition;
// #else
//         if (Input.touchCount > 0)
//         {
//             Touch touch = Input.GetTouch(0);
//             //text2.text = touch.position.ToString();
//             var position = touch.position;
//             //var position = new Vector2(touchPosition.x / Screen.width, touchPosition.y / Screen.height);
// #endif
//             Debug.DrawRay(Camera.gameObject.transform.position, Camera.ScreenPointToRay(position).direction * 10, Color.cyan);
//             if (Physics.Raycast(Camera.ScreenPointToRay(position), out RaycastHit hit))
//             {
//                 Debug.Log(hit.textureCoord);
//                 //清洗
//                 Vector2 textureCoord = hit.textureCoord; //碰撞位置的uv纹理坐标
//                 int pixelX = (int)(textureCoord.x * DirtMaskTexture.width);
//                 int pixelY = (int)(textureCoord.y * DirtMaskTexture.height);
//
//                 for (int x = 0; x < _dirtBrush.width; x++)
//                 {
//                     for (int y = 0; y < _dirtBrush.height; y++)
//                     {
//                         Color pixelDirt = _dirtBrush.GetPixel(x, y);
//                         Color pixelDirtMask = DirtMaskTexture.GetPixel(pixelX + x, pixelY + y);
//
//                         /*float removedAmount = pixelDirtMask.g - (pixelDirtMask.g * pixelDirt.g);
//                         DirtAmount -= removedAmount;*/
//
//                         DirtMaskTexture.SetPixel(
//                             pixelX + x,
//                             pixelY + y,
//                             new Color(0, pixelDirtMask.g * pixelDirt.g, 0)
//                         );
//                     }
//                 }
//
//                 DirtMaskTexture.Apply();
//             }
//         }
//     }

    public void CleanAllDirt()
    {
        for (int x = 0; x < DirtMaskTexture.width; x++)
        {
            for (int y = 0; y < DirtMaskTexture.height; y++)
            {
                DirtMaskTexture.SetPixel(x, y, Color.clear);
            }
        }

        DirtAmount = 0;
    }


    #region Hint

    private float value = 0;
    private float hintTime = 3;
    private bool isReverse = false;

    public void ShowHint(bool isShowHint)
    {
        if (!isShowHint) return;

        if (hintTime <= 0)
        {
            value = 0;
            Material.SetFloat("_GlowIndex", value);
            hintTime = 3;
            isShowHint = !isShowHint;
            return;
        }

        if (!isReverse)
        {
            value += Time.deltaTime * 5;
        }
        else
        {
            value -= Time.deltaTime * 5;
        }

        if (value > 6)
        {
            isReverse = true;
        }

        if (value < 0)
        {
            hintTime -= 1;
            isReverse = false;
        }

        Material.SetFloat("_GlowIndex", value);
    }

    #endregion
}